/*
 Ajax 三级省市联动
 settings 参数说明
 -----
 url:省市数据josn文件路径
 prov:默认省份
 city:默认城市
 dist:默认地区（县）
 nodata:无数据状态
 required:必选项
 ------------------------------ */
(function($) {
    $.fn.citySelect = function(settings) {
        if (this.length < 1) {
            return;
        };
        // 默认值
        settings = $.extend({
            url : "city.min.js",
            prov : null,
            city : null,
            dist : null,
            nodata : null,
            required : true,
            hkSelect : false,
            disabled : true
        }, settings);

        var box_obj = this;
        var prov_obj = box_obj.find(".prov");
        var city_obj = box_obj.find(".city");
        var dist_obj = box_obj.find(".dist");
        var select_prehtml = (settings.required) ? "" : "<option value=''>请选择</option>";
        var city_json;

        // 赋值市级函数
        var cityStart = function() {
            var prov_id = prov_obj.get(0).selectedIndex;
            if (!settings.required) {
                prov_id--;
            };
            city_obj.empty().attr("disabled", true);
            dist_obj.empty().attr("disabled", true);
            // 遍历赋值市级下拉列表
            temp_html = select_prehtml;
            if (prov_id >= 0 && typeof (city_json.citylist[prov_id].c) != "undefined") {
                $.each(city_json.citylist[prov_id].c, function(i, city) {
                temp_html += "<option value='" + city.n + "'>" + city.n + "</option>";
                });
            }
            city_obj.html(temp_html).attr("disabled", false).css({
                "display" : "",
                "visibility" : ""
            });
            //对hkSelect插件进行支持
            if (city_obj.hasClass('hk-select')) {
                city_obj.trigger('update');
            }
            distStart();
        };

        // 赋值地区（县）函数
        var distStart = function() {
            
            var prov_id = prov_obj.get(0).selectedIndex;
            var city_id = city_obj.get(0).selectedIndex;
            if (!settings.required) {
                prov_id--;
                city_id--;
            };
            dist_obj.empty().attr("disabled", true);
            // 遍历赋值市级下拉列表
            temp_html = select_prehtml;
            if (prov_id >= 0 && city_id >= 0 && typeof (city_json.citylist[prov_id].c[city_id].a) != "undefined"){
                $.each(city_json.citylist[prov_id].c[city_id].a, function(i, dist) {
                    temp_html += "<option value='" + dist.s + "'>" + dist.s + "</option>";
                });
            }
            dist_obj.html(temp_html).attr("disabled", false).css({
                "display" : "",
                "visibility" : ""
            });
            //对hkSelect插件进行支持
            if (dist_obj.hasClass('hk-select')) {
                dist_obj.trigger('update');
            }

        };

        var init = function() {
            // 遍历赋值省份下拉列表
            temp_html = select_prehtml;
            $.each(city_json.citylist, function(i, prov) {
                temp_html += "<option value='" + prov.p + "'>" + prov.p + "</option>";
            });
            prov_obj.html(temp_html);

            // 若有传入省份与市级的值，则选中。（setTimeout为兼容IE6而设置）
            if (settings.prov != null) {
                prov_obj.val(settings.prov);
                cityStart();
                if (settings.disabled) {
                    prov_obj.attr('disabled', true);
                }
                if (settings.city != null) {
                    city_obj.val(settings.city);
                    if (settings.disabled) {
                        city_obj.attr('disabled', true);
                    }
                    distStart();
                    if (settings.dist != null) {
                        dist_obj.val(settings.dist);
                        if (settings.disabled) {
                            dist_obj.attr('disabled', true);
                        }
                    };
                };
            }else{
                cityStart();
            }

            // 选择省份时发生事件
            prov_obj.change(function() {
                cityStart();
            });
            // 选择市级时发生事件
            city_obj.change(function() {
                distStart();
            });
            if (settings.hkSelect) {
                prov_obj.hkSelect();
                city_obj.hkSelect();
                dist_obj.hkSelect();
            }
        };

        // 设置省市json数据
        if ( typeof (settings.url) == "string") {
            $.getJSON(settings.url, function(json) {
                city_json = json;
                init();
            });
        } else {
            city_json = settings.url;
            init();
        };
    };
})(jQuery);
